using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using Model;
using System.Collections.Generic;

public partial class CurrentIdea : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["uid"] == null)
                Response.Redirect("auth.aspx?back=CurrentIdea");
            ViewState["uid"] = Session["uid"];
            ViewState["IID"] = Request.QueryString["IID"];
            IdeaService isc = new IdeaService();
            Idea idea = isc.GetIdeaByID(int.Parse(ViewState["IID"].ToString()), ViewState["uid"].ToString());
            CategoryType.Text = idea.CategoryName;
            Title.Text = idea.Title;
            switch (idea.Status)
            {
                case 1:
                    State.ImageUrl = "images/new.gif";
                    break;
                case 2:
                    State.ImageUrl = "images/beingreviewed.gif";
                    break;
                case 3:
                    State.ImageUrl = "images/returned.gif";
                    break;
                case 4:
                    State.ImageUrl = "images/beingimplemented.gif";
                    break;
            }
            CreatedBy.InnerHtml = idea.FirstName + "." + idea.LastName;
	    CreatedBy.Target = "_blank";
            CreatedBy.HRef = "http://www.colleaguefinder.com/EmployeeProfile.htm?employeeId=" + idea.CreatedBy;
            CreatedTime.Text = idea.CreatedTime;
            VoteNum.InnerText = "(" + idea.Votes.ToString() + ")";


            bool joined = false;
            bool voted = false;
            if (!string.IsNullOrEmpty(idea.MapTypes))
            {
                string[] types = idea.MapTypes.Split('|');
                for (int i = 0; i < types.Length; i++)
                {
                    if (int.Parse(types[i]) == 1)
                        joined = true;
                    if (int.Parse(types[i]) == 2)
                        voted = true;
                }
            }
            if (!joined)
            {
                Join.Attributes["title"] = "contribute to this idea.";
                Join.Attributes["alt"] = "contribute to this idea.";
                Join.Attributes.Add("src", "images/join-button.gif");
                Join.Attributes.Add("onclick", "javascript:join(" + Request.QueryString["i"] + "," + Request.QueryString["IID"] + ");");
            }
            else
            {
                Join.Attributes["title"] = "you've already joined.";
                Join.Attributes["alt"] = "you've already joined.";
                Join.Attributes.Add("src", "images/join-button-gray.gif");
                Join.Attributes.Remove("onclick");
            }
            if (!voted)
            {
                link.Attributes["title"] = "vote.";
                link.Attributes["alt"] = "vote.";
                link.Attributes.Add("src", "images/vote-button.gif");
                link.Attributes.Add("onclick", "javascript:voteForLike('" + ViewState["uid"].ToString() + "'," + ViewState["IID"].ToString() + "); return false;");
            }
            else
            {
                link.Attributes.Remove("onclick");
                link.Attributes.Add("src", "images/vote-button-gray.gif");
                link.Attributes["title"] = "you've already voted.";
                link.Attributes["alt"] = "you've already voted.";
            }
            Content.Text = idea.Content;

            if (!string.IsNullOrEmpty(idea.Attachments))
            {

                string[] attached = idea.Attachments.Split('|');
                attch.Controls.Add(new LiteralControl("<h4>Attachments</h4>"));
                attch.Controls.Add(new LiteralControl("<ul>"));
                for (int i = 0; i < attached.Length; i++)
                {
                    LiteralControl lc = new LiteralControl("<li><a href=\"UploadFiles/" + attached[i] + "\" target=\"_blank\" > File " + (i + 1) + "</a></li>");
                    attch.Controls.Add(lc);
                }

                attch.Controls.Add(new LiteralControl("</ul>"));
            }
            else
                attch.Visible = false;

            CommentService csc = new CommentService();
            IList<Comment> list = csc.GetCommentListByIID(int.Parse(ViewState["IID"].ToString()));
            foreach (Control cl in comments.Controls)
            {
                comments.Controls.Remove(cl);
            }
            comments.Controls.Add(new LiteralControl("<ul>"));
            if (list.Count > 0)
            {
                foreach (Comment c in list)
                {
                    comments.Controls.Add(new LiteralControl("<li id=\"" + c.CID + "\">"));
                    LiteralControl lc_comment = new LiteralControl("<div class=\"comments\"><p>" + c.Content + "</p></div>");
                    LiteralControl lc_post = new LiteralControl("<div class=\"post\">By " + c.FName + "." + c.LName + " at " + c.CreatedTime + "</div>");
                    comments.Controls.Add(lc_comment);
                    comments.Controls.Add(lc_post);
                    comments.Controls.Add(new LiteralControl("</li"));
                }
            }
            else
            {
                comments.Controls.Add(new LiteralControl("<li>no comment.</li>"));
            }
            comments.Controls.Add(new LiteralControl("</ul>"));
        }
    }

    protected void Lbtn_Click(object sender, EventArgs e)
    {
        int i = int.Parse(Request.QueryString["i"]);
        string path = "";
        switch (i)
        {
            case 0:
                path = "Hot.aspx";
                break;
            case 1:
                path = "Latest.aspx";
                break;
            case 2:
                path = "My.aspx";
                break;
            case 3:
                path = "Favorite.aspx";
                break;
        }
        Response.Redirect(path);
    }

    protected void Ibtn_Click(object sender, ImageClickEventArgs e)
    {
        int i = int.Parse(Request.QueryString["i"]);
        string path = "";
        switch (i)
        {
            case 0:
                path = "Hot.aspx";
                break;
            case 1:
                path = "Latest.aspx";
                break;
            case 2:
                path = "My.aspx";
                break;
            case 3:
                path = "Favorite.aspx";
                break;
        }
        Response.Redirect(path);
    }
    protected void Sbtn_ServerClick(object sender, ImageClickEventArgs e)
    {
        CommentService csc = new CommentService();
        Comment comment = new Comment();
        comment.IID = int.Parse(ViewState["IID"].ToString());
        comment.Content = mycomment.InnerText;
        comment.CreatedBy = ViewState["uid"].ToString();
        string hour = System.DateTime.Now.Hour > 9 ? System.DateTime.Now.Hour.ToString() : "0" + System.DateTime.Now.Hour;
        string min = System.DateTime.Now.Minute > 9 ? System.DateTime.Now.Minute.ToString() : "0" + System.DateTime.Now.Minute;
        string sec = System.DateTime.Now.Second > 9 ? System.DateTime.Now.Second.ToString() : "0" + System.DateTime.Now.Second;
        comment.CreatedTime = System.DateTime.Now.ToString("yyyy-MM-dd") + " " + hour + ":" + min + ":" + sec;
        csc.CreateComment(comment);

        IList<Comment> list = csc.GetCommentListByIID(int.Parse(ViewState["IID"].ToString()));
        foreach (Control cl in comments.Controls)
        {
            comments.Controls.Remove(cl);
        }
        comments.Controls.Add(new LiteralControl("<ul>"));
        if (list.Count > 0)
        {
            foreach (Comment c in list)
            {
                comments.Controls.Add(new LiteralControl("<li id=\"" + c.CID + "\">"));
                LiteralControl lc_comment = new LiteralControl("<div class=\"comments\"><p>" + c.Content + "</p></div>");
                LiteralControl lc_post = new LiteralControl("<div class=\"post\">By " + c.FName + "." + c.LName + " at " + c.CreatedTime + "</div>");
                comments.Controls.Add(lc_comment);
                comments.Controls.Add(lc_post);
                comments.Controls.Add(new LiteralControl("</li"));
            }
        }
        else
        {
            comments.Controls.Add(new LiteralControl("<li>no comment.</li>"));
        }
        comments.Controls.Add(new LiteralControl("</ul>"));

        mycomment.InnerText = string.Empty;
    }
}
